package com.ruoyi.womp.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public final static String yyyyMMdd = "yyyy-MM-dd";

	public final static String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

	public static String sysDateTime() {
		return formatDate(Calendar.getInstance().getTime(), yyyyMMddHHmmss);
	}

	public static String formatDate(Date date) {
		return formatDate(date, yyyyMMdd);
	}

	public static String formatDateTime(Date date) {
		return formatDate(date, yyyyMMddHHmmss);
	}

	public static String formatDate(Date date, String dateFormat) {
		DateFormat df = new SimpleDateFormat(dateFormat);
		return df.format(date);
	}

	public static Date parseDate(String date, String format) throws ParseException {
		DateFormat df = new SimpleDateFormat(format);
		Date d = df.parse(date);
		return d;
	}

	public static Date parseDateToUTC(String date, String format) throws ParseException {
		Date d = parseDate(date, format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.HOUR_OF_DAY, 8);
		return cal.getTime();
	}

	public static Date parseDateToUTC1(String date, String format) throws ParseException {
		Date d = parseDate(date, format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.HOUR_OF_DAY, -8);
		return cal.getTime();
	}

	public static String dateAddDay(int days, String format) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, days);

		DateFormat df = new SimpleDateFormat(format);
		return df.format(cal.getTime());
	}

	public static Date dateAddHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, hour);

		return cal.getTime();
	}

	public static String getDateTimeFromTimestamp(Long timestamp) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(timestamp);

		return formatDateTime(cal.getTime());
	}
}
